package com.itboye.eweilisten.me.timu.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.app.MyApplication;
import com.itboye.eweilisten.model.TIbean;
import com.itboye.eweilisten.util.widget.FlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 单选
 */
public class Dandapter extends RecyclerView.Adapter<Dandapter.MyViewHolder> {
    private Context context;
    private List<TIbean> beans;

    public Dandapter(Context context, List<TIbean> beans) {
        this.context = context;
        this.beans = beans;
    }

    @Override
    public Dandapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_danxuans, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.timu_num.setText(beans.get(position).getTitle());
        holder.questionLine.removeAllViews();
        holder.answerLine.removeAllViews();
        for (int j = 0; j < beans.get(position).getContent().size(); j++) {
            questionContentAdd(holder.questionLine, beans.get(position).getContent().get(j));
        }
        for (int j = 0; j < beans.get(position).getOption().size(); j++) {
            answerLineContentAdd(holder.answerLine, beans.get(position).getOption(), beans.get(position).getAnswers().get(j), j, position);
        }
    }

    @Override
    public int getItemCount() {
        return beans.size();
    }

    /**
     * 内容
     * @param questionLine
     * @param string
     */
    private void questionContentAdd(final FlowLayout questionLine, String string) {
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 0, 5);

        if ("_".equals(string)) {
            TextView textView = new TextView(context);
            textView.setText(string + "__");
            textView.setTextSize(16);
            textView.setTextColor(Color.parseColor("#3b7830"));
            textView.setPadding(15, 0, 15, 0);
            textView.setLayoutParams(params);
            questionLine.addView(textView);

        } else {
            TextView textView = new TextView(context);
            textView.setText(string);
            textView.setTextSize(16);
            textView.setTextColor(Color.parseColor("#3b7830"));
            textView.setLayoutParams(params);
            questionLine.addView(textView);
        }
    }

    /**
     * 答案
     */
    private void answerLineContentAdd(final FlowLayout answerLine, final ArrayList<TIbean.Options> qianzhui, String content, final int j, final int position) {
        // 设置添加的textvIew的参数
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        params.setMargins(0, 5, 25, 5);

        final View innerView = LayoutInflater.from(MyApplication.instance).inflate(R.layout.item_a3, null, false);
        final TextView a3_title = (TextView) innerView.findViewById(R.id.a3_title);
        final TextView a3_content = (TextView) innerView.findViewById(R.id.a3_content);
        final ImageView a3_img = (ImageView) innerView.findViewById(R.id.a3_img);
        final RelativeLayout a3_layout = (RelativeLayout) innerView.findViewById(R.id.a3_layout);
        a3_title.setText(qianzhui.get(j).getOptions());
        a3_content.setText(content);
        answerLine.addView(innerView, params);

        if (qianzhui.get(j).isDa()) {
            a3_layout.setBackgroundResource(R.drawable.shape_a33);
            a3_title.setBackgroundResource(R.color.olive_green);
            a3_title.setTextColor(context.getResources().getColor(R.color.white));
            a3_img.setVisibility(View.VISIBLE);
        } else {
            a3_layout.setBackgroundResource(R.drawable.shape_a3);
            a3_title.setBackgroundResource(R.color.gray_D6);
            a3_title.setTextColor(context.getResources().getColor(R.color.blue));
            a3_img.setVisibility(View.GONE);
        }
        innerView.setTag(R.id.tag, qianzhui.get(j).getOptions());
        innerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < qianzhui.size(); i++) {
                    qianzhui.get(i).setDa(false);
                }
                qianzhui.get(j).setDa(true);
                ArrayList<String> answer_truev = beans.get(position).getAnwser_true();
                int poistions = answer_truev.indexOf(innerView.getTag(R.id.tag).toString());
                if (poistions == -1) {
                    beans.get(position).setXuan(1);//1代表选择错误
                } else {
                    beans.get(position).setXuan(0);//0代表选择正确
                }
//                for (int i = 0; i < beans.get(position).getAnwser_true().size(); i++) {
//                   String sss= innerView.getTag(R.id.tag).toString();
//                   String ssss=beans.get(position).getAnwser_true().get(i);
//                    System.out.println("hishsi:"+sss+"a:"+ssss);
//                    if (innerView.getTag(R.id.tag).toString().equals(beans.get(position).getAnwser_true().get(i))) {//如果正确答案和选择的相同
//                        beans.get(position).setXuan(0);
//                        break;
//                    }else{
//                        beans.get(position).setXuan(1);
//                    }
//                }
                notifyDataSetChanged();
            }

        });


    }

    // 自定义的ViewHolder，持有每个Item的的所有界面元素
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public FlowLayout questionLine;
        public FlowLayout answerLine;
        public TextView timu_num;

        public MyViewHolder(View itemView) {
            super(itemView);
            questionLine = (FlowLayout) itemView.findViewById(R.id.questionLine);
            answerLine = (FlowLayout) itemView.findViewById(R.id.answerLine);
            timu_num = (TextView) itemView.findViewById(R.id.timu_num);
        }
    }
}
